package org.oneblog.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.oneblog.data.bean.AlbumSubs;
import org.oneblog.data.bean.AlbumView;
import org.oneblog.data.bean.ArticleAlbum;
import org.oneblog.data.bean.ArticleSolr;
import org.oneblog.data.query.AlbumQuery;
import org.oneblog.data.service.IArticleAlbumSolrService;
import org.oneblog.data.service.IArticleSolrService;
import org.oneblog.data.util.DateFormatUtil;
import org.oneblog.service.IAlbumService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service("albumService")
public class AlbumServiceImpl implements IAlbumService{

	@Override
	public int upsert(ArticleAlbum album) {
		
		return albumSolrService.upsertAlbum(album);
	}

	@Override
	public AlbumView detail(String id) {
		
		ArticleAlbum articleAlbum = albumSolrService.getArticleAlbum(id);
		
		return createFrom(articleAlbum, createFromAlbum(articleAlbum));
	}

	@Override
	public List<AlbumView> getAlbums(AlbumQuery query) {
		
		List<ArticleAlbum> albums = albumSolrService.getAlbums(query);
		
		if(CollectionUtils.isEmpty(albums)) {
			return new ArrayList<>(0);
		}
		
		Map<String, ArticleSolr> map = new HashMap<>(0);
		if(query.getViewSubs() != null && query.getViewSubs() == 1) {
			map = createFromAlbum(albums);
		}
		List<AlbumView> views = new ArrayList<>(albums.size());
		for(ArticleAlbum a : albums) {
			AlbumView view = createFrom(a, map);
			if(view != null) {
				
				views.add(view);
			}
		}
		
		return views;
	}
	
	private Map<String, ArticleSolr> createFromAlbum(List<ArticleAlbum> albums) {
		if(CollectionUtils.isEmpty(albums)) {
			return null;
		}
		
		Set<String> ids = new HashSet<>(albums.size() * 4);
		for(ArticleAlbum b : albums) {
			ids.addAll(b.getLinks());
		}
		return createMapFrom(new ArrayList<>(ids));
	}
	
	private Map<String, ArticleSolr> createFromAlbum(ArticleAlbum album) {
		if(album == null) {
			return null;
		}
		
		return createMapFrom(album.getLinks());
	}
	
	private Map<String, ArticleSolr> createMapFrom(List<String> articleIds) {
		Map<String, ArticleSolr> temp = new HashMap<>();
		
		List<ArticleSolr> list = articleSolrService.getArticlesForIds(articleIds);
		
		if(CollectionUtils.isEmpty(list)) {
			return temp;
		}
		for(ArticleSolr a : list) {
			temp.put(a.getArticleId(), a);
		}
		
		return temp;
	}
	
	private AlbumView createFrom(ArticleAlbum album, Map<String, ArticleSolr> map) {
		AlbumView view = new AlbumView();
		
		if(album == null) {
			return view;
		}
		
		view.setId(album.getAlbumId());
		view.setTitle(album.getAlbumName());
		view.setInfo(album.getInfo());
		view.setCreateTimeString(DateFormatUtil.formatLong(album.getCreateTime()));
		view.setLogo(album.getLogo());
		view.setDefunct(album.getDefunct());
		
		List<String> links = album.getLinks();
		if(CollectionUtils.isEmpty(links)) {
			return view;
		}
		List<AlbumSubs> subs = new ArrayList<>(links.size());
		for(String id : links) {
			ArticleSolr articleSolr = map.get(id);
			if(articleSolr == null) {
				continue;
			}
			AlbumSubs sub = new AlbumSubs();
			sub.setArticleId(id);
			sub.setArticleTitle(articleSolr.getArticleTitle());
			sub.setCreateTime(DateFormatUtil.formatOnDay(new Date(articleSolr.getCreateTime())));
			sub.setImageUrl(articleSolr.getThumbnailImgUrl());
			sub.setContent(articleSolr.getArticleContent());
			sub.setTags(articleSolr.getTags());
			sub.setSrcType(articleSolr.getSrcType());
			sub.setViewCount(articleSolr.getBrowserCount());
			sub.setUpdateTimeString(DateFormatUtil.formatOnDay(new Date(articleSolr.getUpdateTime())));
			sub.setGoodCount(articleSolr.getGoodCount());
			subs.add(sub);
		}
		view.setArticles(subs);
		return view;
	}
	
	@Override
	public int deleteAlbum(String id) {
		return albumSolrService.updateAlbumDefunct(id, 1);
	}
	@Override
	public int recoverAlbum(String id) {
		return albumSolrService.updateAlbumDefunct(id, 0);
	}

	@Resource(name = "articleAlbumSolrService")
	private IArticleAlbumSolrService albumSolrService;

    @Resource(name = "articleSolrService")
    private IArticleSolrService articleSolrService;
}
